package com.zretc.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.codec.digest.DigestUtils;

import com.google.gson.Gson;
import com.zretc.entity.CollectionInfo;
import com.zretc.entity.OrderInfo;
import com.zretc.service.OrderAndCollectionService;
import com.zretc.service.UserService;
import com.zretc.service.impl.OrderAndCollectionServiceImpl;
import com.zretc.service.impl.UserServiceImpl;
import com.zretc.util.PageInfo;

/**
 * Servlet implementation class UserUpdateServlet
 */
@WebServlet("/UserUpdateServlet")
public class UserUpdateServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private UserService userService = new UserServiceImpl();
	private OrderAndCollectionService oacs = new OrderAndCollectionServiceImpl();
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UserUpdateServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		String op = request.getParameter("op");
		System.out.println("op "+op);
		if("updatePassword".equals(op)) {
			updatePassword(request,response);
		}else if ("outLogin".equals(op)) {
			outLogin(request,response);
		}else if ("deleteByOrderNo".equals(op)) {
			doDeleteByOrderNo(request,response);
		}else if ("deleteByCollectionNo".equals(op)) {
			doDeleteByCollectionNo(request,response);
		}else if ("getOrderListByPage".equals(op)) {
			doGetOrderListByPage(request,response);
		}else if ("getCollectionListByPage".equals(op)) {
			doGetCollectionListByPage(request,response);
		}
	}
	
	private void doGetCollectionListByPage(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// 1.调用模型层,获取数据
		String homeNameKey = request.getParameter("homeNameKey");  // ""或"值"
		System.out.println("homeNameKey  "+homeNameKey);
		String pageNum = request.getParameter("pageNum");
		System.out.println("pageNum:"+pageNum);
		String pageSize = request.getParameter("pageSize");
		
		Map<String, String> map = new HashMap<>();
		map.put("homeName", homeNameKey);
		map.put("pageNum", pageNum);
		map.put("pageSize", pageSize);
		PageInfo<CollectionInfo> pageInfo = new OrderAndCollectionServiceImpl().findCollectionByPage(map);
		
		// 利用第三方类库,将数据转换成JSON字符串
		Gson gson = new Gson();
		String jsonObject = gson.toJson(pageInfo);
		// 2.响应
		response.setContentType("application/json");
		response.getWriter().write(jsonObject);
	}

	private void doGetOrderListByPage(HttpServletRequest request, HttpServletResponse response) throws IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		// 1.调用模型层,获取数据
		String homeNameKey = request.getParameter("homeNameKey");  // ""或"值"
		String userNo = request.getParameter("userNo");      // ""或"值"
		System.out.println("房间号："+userNo);
		String num = request.getParameter("num");
		System.out.println("num"+num);
		String pageNum = request.getParameter("pageNum");
		String pageSize = request.getParameter("pageSize");
		
		Map<String, String> map = new HashMap<>();
		map.put("homeName", homeNameKey);
		map.put("userNo", userNo);
		map.put("num", num);
		map.put("pageNum", pageNum);
		map.put("pageSize", pageSize);
		
		PageInfo<OrderInfo> pageInfo = new OrderAndCollectionServiceImpl().findOrderByPage(map);
		
		// 利用第三方类库,将数据转换成JSON字符串
		Gson gson = new Gson();
		String jsonObject = gson.toJson(pageInfo);
		// 2.响应
		response.setContentType("application/json");
		response.getWriter().write(jsonObject);
	}

	private void doDeleteByCollectionNo(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String collectionNo = request.getParameter("collectionNo");
		System.out.println("删除collectionID："+collectionNo);
		oacs.deleteCollectionByCollectionNo(collectionNo);
		
		// 再调用一次查询收藏信息的方法
		doGetCollectionListByPage(request, response);
	}
	
	private void doDeleteByOrderNo(HttpServletRequest request, HttpServletResponse response) throws IOException {
		
		String orderNo = request.getParameter("orderNo");
		System.out.println("删除orderID："+orderNo);
		oacs.deleteOrderByOrderNo(orderNo);
		
		// 再调用一次查询订单信息的方法
		doGetOrderListByPage(request, response);
	}

	private void outLogin(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// 清除session中属性信息
		HttpSession session = request.getSession();
		session.removeAttribute("user");
		// 响应
		response.getWriter().write("ok");
		
	}

	private void updatePassword(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// 获取用户的ID
		String userNo = request.getParameter("userNo");
		// 获取用户修改的密码
		String newPwd = request.getParameter("newPwd1");
		System.out.println("新密码："+newPwd);
		// 对密码进行加密，加密后再通过Hibernate往数据库里存
		String userPassword = DigestUtils.md5Hex(newPwd);
		System.out.println("加密之后的密码：" + userPassword);
		// 调用业务层中的修改密码方法
		boolean updateUserPassword = userService.updateUserPassword(userNo, userPassword);
		
		Gson gson = new Gson();
		String jsonObject = gson.toJson(updateUserPassword);
		response.getWriter().write(jsonObject);
	}
}
